package WWTPAgent;

import jade.content.ContentManager;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.WakerBehaviour;
import jade.lang.acl.ACLMessage;
import wwtp.ontology.SendWater;
import wwtp.ontology.Water;
import DataBases.WaterTreatmentObject;

public class WaterTreatmentBehaviour extends OneShotBehaviour{

	private int state = 0;

	protected ContentManager manager;
	ACLMessage waterMsg;

	public WaterTreatmentBehaviour(ACLMessage msg) {
		// TODO Auto-generated constructor stub
		this.waterMsg = msg;
	}

	@Override
	public void action() {
		// TODO Auto-generated method stub
		manager = myAgent.getContentManager();
		try{
			SendWater sw = (SendWater)manager.extractContent(waterMsg);
			
			final Water water = sw.getWater();
			//simulamos el tratamiento del agua cuando hayan transcurrido tiempo_tratamiento segundos
			final WaterTreatmentObject wto = ((WWTPAgent)myAgent).dbWaterTreatment.getTreatment(water);
			int tiempo_tratamiento = wto.getTreatmentTime();
			System.out.println(myAgent.getLocalName() + " Starting Treatment : "+ tiempo_tratamiento/1000 + " Seconds");
			myAgent.addBehaviour(new WakerBehaviour(myAgent,tiempo_tratamiento) {

				@Override
				protected void handleElapsedTimeout() {
					// TODO Auto-generated method stub
					((WWTPAgent)myAgent).treatment(water,wto);
				}
			});

		}
		catch (Exception e){
			e.printStackTrace();
		}
	}

}
